﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using internal_website.Models;
using log4net;

namespace internal_website.Controllers
{
    public class CompositionGenreController : Controller
    {

        private static readonly ILog log = LogManager.GetLogger(typeof(ComposerController));
        private OrchestraSiteManagerContext db = new OrchestraSiteManagerContext();

        //
        // GET: /CompositionGenre/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DataTablesAjaxHandler(Models.jQueryDataTables.jQueryDataTableParamModel param)
        {
            //http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
            var filteredEntities = SearchCollectionBy(param.sSearch);
            var displayedEntities = filteredEntities.Skip(param.iDisplayStart).Take(param.iDisplayLength);

            var jsonResult = Json(new
            {
                param.sEcho,
                iTotalRecords = EntireCollection().Count(),
                iTotalDisplayRecords = filteredEntities.Count(),
                aaData = displayedEntities
            }, JsonRequestBehavior.AllowGet);
            return jsonResult;
        }

        private IEnumerable<string[]> SearchCollectionBy(string sSearch)
        {
            var searchTerm = new System.Text.RegularExpressions.Regex(sSearch == null ? ".*" : sSearch);
            var entireCollection = EntireCollection();

            var query = from c in entireCollection
                        where searchTerm.Matches(c.Name).Count > 0
                        select new[] { Convert.ToString(c.Id), Convert.ToString(c.Active), c.Name};

            return query;
        }

        private IEnumerable<CompositionGenre> EntireCollection()
        {
            var entireCollection = from c in db.CompositionGenres
                                   orderby c.Name
                                   select c;

            return entireCollection;
        }







        private CompositionGenre GetCompositionGenreBy(int compositionGenreID = 0)
        {
            var compositionGenreByCompositionGenreId = (from c in db.CompositionGenres
                                                        where c.Id == compositionGenreID
                                                        select c).SingleOrDefault();

            return compositionGenreByCompositionGenreId;
        }


        public ActionResult _Details(int id = 0)
        {
            var compositionGenre = GetCompositionGenreBy(id);
            return PartialView("_Details", compositionGenre);
        }

        #region Create

        public ActionResult _Create()
        {
            return PartialView();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Create(CompositionGenre compositionGenre)
        {
            bool created = false;
            try
            {
                if (ModelState.IsValid)
                {
                    db.CompositionGenres.Add(compositionGenre);
                    db.SaveChanges();
                    created = true;
                }
            }
            catch (Exception e)
            {
                log.Error(e);
                return Json(e.Message);
            }
            return Json(created);
        }

        #endregion

        #region Edit

        public ActionResult _Edit(int id = 0)
        {
            CompositionGenre compositionGenre = db.CompositionGenres.Find(id);
            if (compositionGenre == null)
            {
                return HttpNotFound();
            }
            return PartialView("_Edit", compositionGenre);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(CompositionGenre compositionGenre)
        {
            bool edited = false;
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(compositionGenre).State = EntityState.Modified;
                    db.SaveChanges();
                    edited = true;
                }
            }
            catch (Exception e)
            {
                log.Error(e);
                return Json(e.Message);
            }
            return Json(edited);
        }

        #endregion

        #region Delete

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Delete(int id)
        {
            bool deleted = false;
            try
            {
                if (ModelState.IsValid)
                {
                    CompositionGenre composer = db.CompositionGenres.Find(id);
                    db.CompositionGenres.Remove(composer);
                    db.SaveChanges();
                    deleted = true;
                }
            }
            catch (Exception e)
            {
                log.Error(e);
                return Json(e.Message);
            }
            return Json(deleted);
        }

        #endregion

        #region EmptyFormWithAntiForgeryToken

        public ActionResult Get_EmptyFormWithAntiForgeryToken()
        {
            return PartialView("_EmptyFormWithAntiForgeryToken");
        }

        #endregion

        #region Dispose

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #endregion
    }
}